#!/usr/bin/env python

"""
Script to verify that the case is ready for submission.

Typical usage is simply:
   ./check_case

You can run this before running case.submit to:
  - Ensure that all of the env xml files are in sync with the locked files
  - Create namelists (thus verifying that there will be no problems with
    namelist generation)
  - Ensure that the build is complete

Running this is completely optional: these checks will be done
automatically when running case.submit. However, you can run this if you
want to perform these checks without actually submitting the case.
"""

from standard_script_setup import *

from CIME.utils import expect
from CIME.case  import Case

import argparse

logger = logging.getLogger(__name__)

###############################################################################
def parse_command_line(args, description):
###############################################################################

    parser = argparse.ArgumentParser(
        description=description,
        formatter_class=argparse.RawTextHelpFormatter)

    CIME.utils.setup_standard_logging_options(parser)

    args = CIME.utils.parse_args_and_handle_standard_logging_options(args, parser)

###############################################################################
def _main_func(description):
###############################################################################
    parse_command_line(sys.argv, description)

    with Case(read_only=False) as case:
        case.check_lockedfiles()
        case.create_namelists()
        build_complete = case.get_value("BUILD_COMPLETE")

    if not build_complete:
        expect(False,
               "Please rebuild the model interactively by calling case.build")

    logger.info( "check_case OK ")

###############################################################################

if __name__ == "__main__":
    _main_func(__doc__)
